using UnityEngine;
using System.Collections;

public class SpriteAnimation : MonoBehaviour {
	
	public UIAtlas animationUseAtlas;
	public int frameInterval = 6;// When would next frame play.	
	
	private int FrameCounter;// Count frames elapsed since last change.
	private int CurrentFrame;
	
	private bool FaceLeft;
	
	private int StartFrame;
	private int EndFrame;
	UISprite ui;
	
	public int groundStandLeftStartFrame = 7;
	public int groundStandLeftEndFrame = 7;
	
	public int groundWalkLeftStartFrame = 6;
	public int groundWalkLeftEndFrame = 9;
	
	public int airLeftStartFrame = 1;
	public int airLeftEndFrame = 1;
	
	
	public int groundStandRightStartFrame = 3;
	public int groundStandRightEndFrame = 3;
	
	public int groundWalkRightStartFrame = 2;
	public int groundWalkRightEndFrame = 5;
	
	public int airRightStartFrame = 0;
	public int airRightEndFrame = 0;
	
	// Use this for initialization
	void Start () {
	
		FrameCounter = 0;		
		//frameInterval = 6;		
		CurrentFrame = 0;
		
		// Default Status
		FaceLeft = false;
		
		ui = gameObject.GetComponent<UISprite>();
		if(animationUseAtlas != null)
			ui.atlas = animationUseAtlas;
		
		//Debug.Log ("Sprite count" + ui.atlas.spriteList.Count);
		
		string name = ui.atlas.spriteList[CurrentFrame].name;
		
		ui.spriteName = name;
		
		ui.depth = 2;
	}
	
	// Update is called once per frame
	void Update () {
		
		GetInput ();
		
		PlayAnimation ();
		
	}// void Update ()
	
	private void GetInput()
    {
		if(Input.GetKey(KeyCode.LeftArrow))
		{
            //movement = -1.0f;
			//Debug.Log ("Salm:<=");
			
			if(gameObject.GetComponent<playerCharacter>().IsOnGround)
			{
				StartFrame = groundWalkLeftStartFrame;
				EndFrame = groundWalkLeftEndFrame;
			}
			else
			{
				StartFrame = airLeftStartFrame;
				EndFrame = airLeftEndFrame;
			}
			FaceLeft = true;
        }
		else if(Input.GetKey(KeyCode.RightArrow))
		{
            //movement = 1.0f;
			//Debug.Log ("Salm:=>");

			if(gameObject.GetComponent<playerCharacter>().IsOnGround)
			{
				StartFrame = groundWalkRightStartFrame;
				EndFrame = groundWalkRightEndFrame;
			}
			else
			{
				StartFrame = airRightStartFrame;
				EndFrame = airRightEndFrame;
			}
			FaceLeft = false;
        }
		else if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.Space))
		{
			
			if (FaceLeft)
			{
				StartFrame = airLeftStartFrame;
				EndFrame = airLeftEndFrame;
			}
			else
			{
				StartFrame = airRightStartFrame;
				EndFrame = airRightEndFrame;
			}
		}
		else
		{
			if (gameObject.GetComponent<playerCharacter>().IsOnGround)
			{
				if (FaceLeft)
				{
					StartFrame = groundStandLeftStartFrame;
					EndFrame = groundStandLeftEndFrame;
				}
				else
				{
					StartFrame = groundStandRightStartFrame;
					EndFrame = groundStandRightEndFrame;
				}
			}
			else 
			{
				if (FaceLeft)
				{
					StartFrame = airLeftStartFrame;
					EndFrame = airLeftEndFrame;
				}
				else
				{
					StartFrame = airRightStartFrame;
					EndFrame = airRightEndFrame;
				}
			}
		}
		
    }// private void GetInput()
	
	private void PlayAnimation()
	{
		if (CurrentFrame < StartFrame)
		{
			CurrentFrame = StartFrame;
		}
		else
		{
			if (FrameCounter < frameInterval)
			{
				FrameCounter += 1;
			}
			else
			{
	
				if (CurrentFrame < EndFrame)
				{
					CurrentFrame += 1;
		
					string name = ui.atlas.spriteList[CurrentFrame].name;

					ui.spriteName = name;

					ui.depth = 2;
				}
				else 
				{
					CurrentFrame = StartFrame;
			
					string name = ui.atlas.spriteList[CurrentFrame].name;

					ui.spriteName = name;

					ui.depth = 2;
				}
				FrameCounter = 0;
			}
		}
	}// private void PlayAnimation()
	
}
